﻿#ifndef CVlcOper_H
#define CVlcOper_H
#include <QWidget>
#include <QMutex>
#include <QLabel>
#include <QTimer>
#include <QImage>
#include <qpointer.h>
#include <QPen>
#include "vlc/vlc.h"
#include "CDevDataCam.h"
class CVlcOper : public QObject
{
	Q_OBJECT
public:
	CVlcOper(CDevDataCam* pDevDataCam, const QString& url);
	~CVlcOper();
	bool init();   //初始化
	void setImageWidget(QWidget* pWidget = nullptr);
	bool startCapture();
	bool stopCapture();
	void setCorrectData(double dLeftRectifyValue, double dRightRectifyValue, int nRectifySetValue, int nRectifySetValueTwo, QPointF BeltEdgePf1, QPointF BeltEdgePf2, QPointF BeltEdgePf3, QPointF BeltEdgePf4,
		int nIsHaveBeltState, bool bBeltState);
private:
	static QPen getPenForRectifyValue(double rectifyValue, double setValueTwo, double setValue, int nHaveBeltState, bool bBeltState);
	bool stopVlcPlayerSafe(libvlc_media_player_t* player, int timeoutMs = 2000);
	static void* lockCallback(void* opaque, void** planes);
	static void unlockCallback(void* opaque, void* picture, void* const* planes);
	static void displayCallback(void* opaque, void* picture);
private:
	QMutex m_cImageLock;
	uchar* m_rawBuffer = nullptr;     // VLC 写入
	QImage* m_pCurrentFrame;
	libvlc_instance_t* m_pVlcInstance;
	libvlc_media_player_t* m_pMediaPlayer;
	libvlc_media_t* m_pMedia;
	QPointer<QLabel> m_pImageLabel;
	QString m_sUrl;
	bool m_bVideoRun;
	bool m_bInit;
	int m_nWidth;   //图片宽
	int m_nHeight;  //图片高
	int m_bytes = 0;

	//跑偏画线和左下角文字相关
	QMutex m_cDataLock;
	QPointF m_BeltEdgePf1;//皮带边缘点1 左上
	QPointF m_BeltEdgePf2;//皮带边缘点2 右上
	QPointF m_BeltEdgePf3;//皮带边缘点3 右下
	QPointF m_BeltEdgePf4;//皮带边缘点4 左下
	double m_dLeftRectifyValue;//左偏值
	double m_dRightRectifyValue;//右偏值
	int m_nRectifySetValue;//一级跑偏判定值
	int m_nRectifySetValueTwo;//二级跑偏判定值
	int m_nIsHaveBeltState;//是否有皮带运行状态
	int m_bBeltState;//皮带运行状态
};

#endif